<?php
/*
	Project:
		RedirectBuilder

	File Version:
		0.1b

	Author(s):
		John Jimenez

	License:
		This script is release under the GPL licence.
		http://opensource.org/licenses/gpl-license.php

	Description:
		[Description]

	Notes:
		[Notes]

	Testing:
		[testing]
*/

$include_errors = true;

//Get Input
while (!feof(STDIN)) {
  $contents .= fread(STDIN, 8192);
}

if ($contents) {
	$op ='';
	$mod_rewrite_op = "";
	$op_404 = '';
	$error_report = '';
	
	// Clean up on the data before splitting it into an array
	$contents = str_replace('%26','&',$contents); // Replace %26 with &
	$contents = str_replace('%27','\'',$contents); // Replace %27 with '
	$contents = str_replace('%2C',',',$contents); // Replace %2c with ,

	$lines = explode("\n",$contents);
	
	// Reverse Alpha the Lines
	natcasesort($lines);
	$lines = array_reverse($lines);
	
	
	$lastLineSource = $lastLineDest = '';
	foreach ($lines as $line) {
		
		$include = true;
		
		$fields = explode("\t",$line);
		
		if (!empty($fields[0])) {
			
			if ($fields[1] == 'KILL') {
				$op_404 .= "Redirect 404 ".$fields[0]."\n";
			}
			else {
				
				// Strip Trailing Slash on source
				if ( substr($fields[0],-1) == '/' ) {
					$fields[0] = substr($fields[0],0,-1);
				}
				
				// URL Decode both
				$fields[0] = urldecode($fields[0]);
				$fields[1] = urldecode($fields[1]);
				
				// Force leading string on destination
				if ( substr($fields[1],0,1) != '/' ) {
					$fields[1] = '/'.$fields[1];
				}
				
				// Comment out case swaps
				if (strtolower($fields[0]).'/' == strtolower($fields[1])) {
					$include = false;
					$error_report .= "# ".$fields[0]." is a case swap and will cause an infinite loop.\n";
				}
				
				// See if the current line matches the last line
				if ($lastLineSource == strtolower($fields[0])) {
					$include = false;
					
					$error_report .= "# ".$fields[0]." is a duplicated source";
					
					if ($lastLineDest != strtolower($fields[1])) {
						$error_report .= " with different destinations: \n# \t\"".$lastLineDest."\" &\n# \t\"".$fields[1].'"';
					}
					$error_report .= "\n";
				}
				else {
					$lastLineSource = strtolower($fields[0]);
					$lastLineDest = strtolower($fields[1]);
				}
				
				$fields[0] = strtolower($fields[0]);
				
				if (strpos($fields[0],'?') !== false) {
					$include = false; // Stop this from going into the regular redirects
					$mod_rewrite_op .= "RewriteCond %{REQUEST_URI}?%{QUERY_STRING} ^".preg_quote($fields[0])."$ [NC]\n";
					$mod_rewrite_op .= "RewriteRule .* ".preg_quote($fields[1])."? [R=301,L]\n";
					$mod_rewrite_op .= "\n";
				}
				
				// Only add the line if there is no issue
				if ($include == true) {
					$fields[1] = str_replace('$','\\$',$fields[1]); // To avoid variable replacment in the Redirects
					$op .= "RedirectMatch 301 (?i)^".preg_quote($fields[0]).'/?$'." ".$fields[1]."\n";
				}
			}
		}

		
	}
	
	if ($op_404) {
		echo "# 404s\n";
		echo $op_404;
		echo "\n\n";
	}
	
	if ($op) {
		echo "# Straight Redirects\n";
		echo $op;
		echo "\n\n";
	}

	if ($mod_rewrite_op) {
		echo "# Rewrites\n";
		echo "RewriteEngine On\n";
		echo $mod_rewrite_op;
		echo "\n\n";
	}
	
	if ($error_report && $include_errors) {
		echo "######\n# ERRORS\n######\n\n";
		echo $error_report;
		echo "\n\n";
	}
}

?>